/*
 * File   : includes/linkedlist.h
 * Author : Nandakiran Kirubanandan
 *
 * This file contains the function declarations for the linked list operations.
 *
 */

#ifndef __linkedlist_h
#define __linkedlist_h
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/*!
 * Structure of the linked list
 *
 */
struct list {
	char *data;
	struct list *next;
	struct list *prev;
};

/*!
 * Linked list class
 *
 */
class linkedlist {
	private:
		
		/*!
		 * Function Name : regeneratebacklinks
		 *      Reconsturct the prev-pointer from scratch
		 * Parameters    : none
		 * Return Type   : void
		 *
		 */

		void regeneratebacklinks();
	public:
		/*!
		 * Linked list pointers 
		 *
		 */
		struct list *first,*last;

		/*! 
		 * Constructor
		 *
		 */
		linkedlist(); 
		
		/*!
		 * Destructor
		 *
		 */
		~linkedlist(); 
		
		/*!
		 * Function Name : add
		 *      Add an item to the linked list.
		 * Parameters    :
		 * 		st	  : string
		 *                      Item to add.
		 * Return Type   : void
		 *
		 */
		void add(char *st);

		/*!
		 * Function Name : count
		 *      Returns the number of items in the linked list.
		 * Parameters    : none
		 * Return Type   : long
		 *
		 */
		long count();

		/*!
		 * Function Name : isEmpty
		 *      Returns the whether the linked list is empty or not.
		 * Parameters    : none
		 * Return Type   : bool
		 *
		 */
		bool isEmpty();

		/*!
		 * Function Name : removeAll
		 *      Clears the linked list.
		 * Parameters    : none
		 * Return Type   : void
		 *
		 */
		void removeAll(); 
		
		/*!
		 * Function Name : remove
		 *      Removes an element from the list and returns it.
		 * Parameters    : none
		 * Return Type   : string
		 * 			The item removed
		 *
		 */
		char *remove();
		
		/*!
		 * Function Name : removekey
		 *      Remove the item specified from the list.
		 * Parameters    : 
		 * 		key	  : string
		 *                      Item to remove.
		 * Return Type   : void
		 *
		 */
		void removekey(char *key); 

		/*!
		 * Function Name : exists
		 *      Returns whether a specified element is in the list.
		 * Parameters    : 
		 * 		key	  : string
		 *                      Item to search.
		 * Return Type   : bool
		 * 			Whether the item was found or not.
		 *
		 */
		bool exists(char *key);

		/*!
		 * Function Name : list
		 *      Print all elements of the list.
		 * Parameters    : none
		 * Return Type   : none
		 *
		 */
		void list();
		
		/*!
		 * Function Name : toString
		 *      Return a string containing all elements of the list.
		 * Parameters    : none
		 * Return Type   : string
		 * 			A space (' ') delimited string containing
		 * 			all the elements.
		 *
		 */
		char* toString(); 
		
		/*!
		 * Function Name : toCSVString
		 *      Return a string containing all elements of the list.
		 * Parameters    : none
		 * Return Type   : string
		 * 			A Comma Separated (',') string containing
		 * 			all the elements.
		 *
		 */
		char* toCSVString(); 
		
		/*!
		 * Function Name : toString
		 *      Return a string containing all elements of the list.
		 * Parameters    : string
		 * Return Type   : string
		 * 			A separator (<sep>) delimited string containing
		 * 			all the elements.
		 *
		 */
		char* toString(char* sep); 

		/*!
		 * Function Name : copyList
		 * 	Return a duplicate linked list containing all elements of the list.
		 * Parameters    : none
		 * Return Type   : linkedlist*
		 * 			A linkedlist containing all the elements.
		 *
		 */

                /*!
		 * Function Name : getdata
		 * Return a data pointed by first node in the list.
		 * Parameters    : none
		 * Return Type   : char*
		 */

		char* getdata();


		linkedlist* copyList();
		
		/*!
		 * Function Name : intersect
		 *      Perform Set intersection with the list specified and return the resulting set.
		 * Parameters    : 
		 * 		ls	  : *linkedlist
		 *                      The second set with which to perform the operation.
		 * Return Type   : linkedlist*
		 * 			The resultant set.
		 *
		 */
		linkedlist* intersect(linkedlist *ls);
		
		/*!
		 * Function Name : minus
		 *      Perform Set minus with the list specified and return the resulting set.
		 * Parameters    : 
		 * 		ls	  : *linkedlist
		 *                      The second set with which to perform the operation.
		 * Return Type   : linkedlist*
		 * 			The resultant set.
		 *
		 */
		linkedlist* minus(linkedlist *ls);

		/*!
		 * Function Name : selfintersect
		 *      Perform Set intersection with the list specified and become the resulting set.
		 * Parameters    : 
		 * 		ls	  : *linkedlist
		 *                      The second set with which to perform the operation.
		 * Return Type   : void
		 *
		 */
		void selfintersect(linkedlist *ls);

		/*!
		 * Function Name : selfminus
		 *      Perform Set minus with the list specified and become the resulting set.
		 * Parameters    : 
		 * 		ls	  : *linkedlist
		 *                      The second set with which to perform the operation.
		 * Return Type   : void
		 *
		 */
		void selfminus(linkedlist *ls);
		
		/*!
		 * Function Name : sortedselfminus
		 *      Perform Set minus  with the list specified and become the resulting set.
		 *      Sort the sets prior to the operation
		 * Parameters    : 
		 * 		ls	  : *linkedlist
		 *                      The second set with which to perform the operation.
		 * Return Type   : void
		 *
		 */
		void sortedselfminus(linkedlist *ls);

		/*!
		 * Function Name : sortedselfunios
		 *      Perform Set union  with the list specified and become the resulting set.
		 *      Sort the sets prior to the operation
		 * Parameters    : 
		 * 		ls	  : *linkedlist
		 *                      The second set with which to perform the operation.
		 * Return Type   : void
		 *
		 */
		void sortedselfunion(linkedlist *ls);

		/*!
		 *
		 * Function Name : sort
		 *      Sort the list
		 * Parameters    : none
		 * Return Type   : void
		 *
		 */
		void sort();
		
		/*!
		 * Function Name : merge
		 *      Merge two linked lists.
		 * Parameters    : 
		 * 		ls	  : *linkedlist
		 *                      The list to merge with.
		 * Return Type   : void
		 *
		 */
		void merge(linkedlist *ls);

		/*!
		 * Function Name : removeDuplicates
		 *      Removes duplicate elements in the list.
		 * Parameters    : none 
		 * Return Type   : void
		 *
		 */
		void removeDuplicates();

		/*!
		 * Function Name : cmp
		 *      Compare two elements of the list.
		 * Parameters    : 
		 * 		a	  : *list
		 * 		b	  : *list
		 *                      The elements to compare.
		 * Return Type   : int
		 * 			The result of the comparison
		 *
		 */
		int cmp(struct list *a,struct list *b);

		bool saveToFile();
};


/*!
 * Function Name : beginsWith
 *      Return whether a string begins with the specified substring.
 * Parameters    : 
 * 		src	  : string
 * 				The Original string which has to be checked.
 * 		substr	  : string
 * 				The substring to begin with.
 * Return Type   : bool
 * 			The result of the comparison
 *
 */
bool beginsWith(char* src, char* substr);

#endif

